#include "ComView.h"
#include "game/PockerCard.h"


SelectPockPanel::SelectPockPanel(){
    m_bSelectable=true;
}
 

bool SelectPockPanel::init(){
     if(!TPanel::init()){
         return false;
     }
     
     return true;
}
bool SelectPockPanel::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
    
    if(!m_bSelectable){
        return false;
    }
    
    clearSelect();
    for(int i=gTouchItems->count()-1;i>=0;i--){ 
        TPanel* pNode =  (TPanel*)(gTouchItems->objectAtIndex(i));
        pNode->setDirty(true);
    }
    
    if(gTouchItems->count()<=0){
        return false;
    }
    
    
    CCNode* item=(CCNode*) gTouchItems->objectAtIndex(0);
    if(item==NULL || item->getParent()==NULL){
        return false;
    }
    
    CCPoint targetPos=getPosInTarget(pTouch->getLocation(),item->getParent());
    
    if(targetPos.y>0 || targetPos.y<-getContentSize().height){
        return false;
    }
    
    PockerCard* node=(PockerCard*) getItemByX(targetPos.x,targetPos.y);

    if(node){ 
        node->setDirty(false);
        node->setSelected();
    }
    return true;
}

void  SelectPockPanel::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
    TPanel::ccTouchMoved(pTouch, pEvent);
    
    
    CCPoint targetPos=getPosInTarget(pTouch->getLocation(),this);
    PockerCard* node=(PockerCard*) getItemByX(targetPos.x,targetPos.y);
    if(node && node->getDirty()){
        node->setDirty(false);
        node->setSelected();
    }
}

CCNode* SelectPockPanel::getItemByX(int nX,int nY){
    for(int i=gTouchItems->count()-1;i>=0;i--){
        
        CCNode* pNode =  (CCNode*)(gTouchItems->objectAtIndex(i));
        
        
        if(pNode->isVisible()){
            bool isFind=true;
             
            
            if(pNode->getScaleX()>0){
                if( nX>pNode->getPositionX() &&
                   nX<pNode->getPositionX()+pNode->getContentSize().width
                   ){
                    
                }else{
                    isFind=false;
                }
            }else {
                if(
                   nX>pNode->getPositionX()-pNode->getContentSize().width &&
                   nX<pNode->getPositionX()
                   ){
                    
                }else{
                    isFind=false;
                }
            }
             
            if(isFind)
                return pNode;
        }
        
    }
    return NULL;
}

void  SelectPockPanel::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
    
    TPanel::ccTouchEnded(pTouch, pEvent);
    
    if(m_pListener&& m_pfnSelector)
        (m_pListener->*m_pfnSelector)(this);
}

void  SelectPockPanel::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent){
    TPanel::ccTouchesCancelled(pTouches, pEvent);
}